package com.dcy.oa.controller;

import com.dcy.common.annotation.Log;
import com.dcy.common.base.controller.DcyBaseController;
import com.dcy.common.model.PageModel;
import com.dcy.common.model.PageResult;
import com.dcy.common.model.R;
import com.dcy.oa.service.LeaveService;
import com.dcy.oa.vo.in.LeaveCreateInVO;
import com.dcy.oa.vo.in.LeaveMeSearchInVO;
import com.dcy.oa.vo.in.LeaveSearchInVO;
import com.dcy.oa.vo.in.LeaveUpdateInVO;
import com.dcy.oa.vo.out.LeaveListOutVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 请假前端控制器
 * </p>
 *
 * @author dcy
 * @since 2021-06-11
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/oa/leave")
@Api(value = "LeaveController", tags = {"请假操作接口"})
public class LeaveController extends DcyBaseController {

    private final LeaveService leaveService;

    @Log
    @ApiOperation(value = "请假分页查询")
    @GetMapping("/page")
    public R<PageResult<LeaveListOutVO>> pageList(LeaveSearchInVO leaveSearchInVO, PageModel pageModel) {
        return success(leaveService.pageListByEntity(leaveSearchInVO, pageModel));
    }

    @Log
    @ApiOperation(value = "我的申请")
    @GetMapping("/getMeLeavePageList")
    public R<PageResult<LeaveListOutVO>> getMeLeavePageList(LeaveMeSearchInVO leaveMeSearchInVO, PageModel pageModel) {
        return success(leaveService.getMeLeavePageList(leaveMeSearchInVO, pageModel));
    }

    @Log
    @ApiOperation(value = "获取详细")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    @GetMapping("/getLeaveById")
    public R<LeaveListOutVO> getLeaveById(@NotBlank(message = "id不能为空") String id) {
        return success(leaveService.getLeaveById(id));
    }

    @Log
    @ApiOperation(value = "保存（不启动流程）")
    @PostMapping("/save")
    public R<Boolean> save(@Validated @ApiParam @RequestBody LeaveCreateInVO leaveCreateInVO) {
        return success(leaveService.save(leaveCreateInVO));
    }

    @Log
    @ApiOperation(value = "提交（启动流程）")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    @PostMapping("/submit")
    public R<Boolean> submit(@Validated @ApiParam @RequestBody LeaveUpdateInVO leaveUpdateInVO) {
        return success(leaveService.submit(leaveUpdateInVO));
    }

    @Log
    @ApiOperation(value = "创建并且提交（启动流程）")
    @PostMapping("/createAndSubmit")
    public R<Boolean> createAndSubmit(@Validated @ApiParam @RequestBody LeaveCreateInVO leaveCreateInVO) {
        return success(leaveService.saveLeave(leaveCreateInVO));
    }

    @Log
    @ApiOperation(value = "重新提交")
    @PostMapping(value = "/reSubmit")
    public R<Boolean> reSubmit(@Validated @ApiParam @RequestBody LeaveUpdateInVO leaveUpdateInVO) {
        return success(leaveService.reSubmitLeave(leaveUpdateInVO));
    }

    @Log
    @ApiOperation(value = "删除审批并且删除流程实例")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@NotBlank(message = "id不能为空") @RequestParam String id) {
        return success(leaveService.deleteLeave(id));
    }

}
